function ClickerAssistant(counter, direction) {
	this.counter = counter;
	this.direction = direction;
}

ClickerAssistant.prototype.setup = function() {
	// Main container
	this.container = this.controller.get('container');
	// Set window orientation
	this.controller.stageController.setWindowOrientation('free');
	// Define application menu
	this.appMenuModel = {
		'visible': true,
		'items': [
			{ 'label': $L("Settings"), 'command': 'settings'},
    		{ 'label': $L("Help"), 'command': 'help'}
		]
	};
	this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);

	// Handler for increase counter
	this.singleTapHandler = this.handleSingleTap.bindAsEventListener(this);
	Mojo.Event.listen(this.container, Mojo.Event.tap, this.singleTapHandler);

	// Handler for reset counter
	this.singleFlickHandler = this.handleSingleFlick.bindAsEventListener(this);
	Mojo.Event.listen(this.container, Mojo.Event.flick, this.singleFlickHandler);

	// Show quick help after launch
	var tips = [
		$L("Tap to count, flick to reset."),
		$L("Set counter from top left menu."),
		$L("Set direction from top left menu.")
	];
	var random = Math.round(Math.random()*2);
	Mojo.Controller.getAppController().showBanner({'messageText': tips[random]}, '', 'clicker');

};

ClickerAssistant.prototype.activate = function(event) {
	// Generate flipper with current counter
	this.generateFlipper(this.counter, this.direction);
};

ClickerAssistant.prototype.deactivate = function(event) {
};

ClickerAssistant.prototype.cleanup = function(event) {
	// Cleanup all handlers
	Mojo.Event.stopListening(this.container, Mojo.Event.tap, this.singleTapHandler);
	Mojo.Event.stopListening(this.container, Mojo.Event.flick, this.singleFlickHandler);
};

ClickerAssistant.prototype.handleCommand = function(event) {
	// Handle application menu
    if(event.type == Mojo.Event.command) {
		switch (event.command) {
			case 'help':
				// About window
				this.controller.stageController.pushAppSupportInfoScene();
			break;
			case 'settings':
				// Prepare variables
				var digits = new String(this.counter);
				var len = digits.length;

				// Fill the string with zeros
				for(var z=4;z>len;z--) {
					digits = '0'+digits;
				}

				// Show settings scene
				this.controller.showDialog({
					'template': 'settings/settings-scene',
					'assistant': new SettingsAssistant(this, digits, this.direction, this.generateFlipper.bind(this))
				});
			break;
		};
	};
};

ClickerAssistant.prototype.handleSingleFlick = function(event) {
	try {
		// Reset on horizontal flick
		if(Math.abs(event.velocity.x) > 1000) {
			this.controller.showAlertDialog({
				'onChoose': function(choice) {
					if(choice) {
						if(this.direction == 'down') {
							this.generateFlipper(9999, this.direction);
						} else {
							this.generateFlipper(0, this.direction);
						}
					}
				}.bind(this),
				'title': $L("Reset counter"),
				'message': $L("Do you really want to reset counter?"),
				'choices': [
					{'label': $L("Yes"), 'value':true, 'type':'negative'},
					{'label': $L("No"), 'value':false, 'type':'primary'}
				]
			});
		};
	} catch(e) {};
};

ClickerAssistant.prototype.handleSingleTap = function(event) {
	if(this.direction == 'down') {
		// Decrease counter
		--this.counter;

		if(this.counter < 0) {
			this.counter = 9999;
		}
	} else {
		// Increase counter
		this.counter++;

		if(this.counter > 9999) {
			this.counter = 0;
		}
	}

	this.generateFlipper(this.counter, this.direction);
};

ClickerAssistant.prototype.generateFlipper = function(counter, direction) {
	// Save counter to storage
	try {
		clickerStorage.simpleAdd('counter', counter);
		clickerStorage.simpleAdd('direction', direction);
	} catch(e) {};

	// Prepare variables
	var digits = new String(counter);
	var len = digits.length;
	var className = 'digit ';
	var z;

	// Fill the string with zeros
	for(z=4;z>len;z--) {
		digits = '0'+digits;
	};
	
	// Set digits
	for(z=0; z<4; z++) {
		this.controller.get('digit-'+(z+1)).className = 'digit number'+digits[z]+(z==3 ? ' last':'');
	};

	// Reparse counter
	this.counter = parseInt(digits, 10);
	this.direction = direction;
};
